package com.kehao.leetcode.jzoffer;

import org.junit.Test;

import java.util.*;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/28 13:39
 */
public class JZSolution033 {

    private String getFeature(String str){
        int[] count = new int[26];
        for (int i = 0; i < str.length(); i++) {
            count[str.charAt(i)-'a']++;
        }
        return Arrays.toString(count);
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<>();
        for(String str:strs){
            List<String> list = map.computeIfAbsent(getFeature(str), key->new ArrayList<>());
            list.add(str);
        }
        return new ArrayList<>(map.values());
    }

    @Test
    public void test01(){
        String[] strList = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = groupAnagrams(strList);
        System.out.println(result);
    }
}
